package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dto.DishDto;
import com.example.demo.entity.Dish;
import com.example.demo.entity.DishFlavor;
import com.example.demo.mapper.DishMapper;
import com.example.demo.service.CategoryService;
import com.example.demo.service.DishFlavorService;
import com.example.demo.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * date 2022年07月19日 19:11
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Resource
    private DishFlavorService dishFlavorService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private RedisTemplate redisTemplate;
    /**
     * 添加菜品
     * @param dishDto
     */
    @Override
    @Transactional
    public void addDish(DishDto dishDto) {
        // 首先封装菜品信息并保存
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish);
        save(dish);
        List<DishFlavor> flavors = dishDto.getFlavors();
        // 封装flavors信息并且批量保存
        flavors =flavors.stream().map((item)->{
            item.setDishId(dish.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public Page<DishDto> pageSearch(int page, int pageSize, String name) {
        // 创建分页
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        // 返回页面的分页
        Page<DishDto> dishDtoPage = new Page<>();
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        if(name != null){
            wrapper.like("name", name);
        }
        // 查出信息
        page(pageInfo,wrapper);
        BeanUtils.copyProperties(pageInfo,dishDtoPage);
        List<Dish> records = pageInfo.getRecords();
        List<DishDto> recordsDto = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            String categoryName = categoryService.getById(item.getCategoryId()).getName();
            dishDto.setCategoryName(categoryName);
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(recordsDto);
        return dishDtoPage;
    }

    /**
     * 根据id回显数据
     * @param id
     * @return
     */
    @Override
    public DishDto getDishById(Long id) {
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id", id);
        DishDto dishDto = new DishDto();
        Dish dish = getById(id);
        List<DishFlavor> dishFlavors = dishFlavorService.list(wrapper);
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    @Override
    public void updateDish(DishDto dishDto) {
        // 首先封装菜品信息并保存
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish);
        updateById(dish);

        List<DishFlavor> flavors = dishDto.getFlavors();
        // 封装flavors信息并且批量保存
        flavors =flavors.stream().map((item)->{
            item.setDishId(dish.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.updateBatchById(flavors);
    }
    /**
     * 根据id删除菜品
     * @param ids
     */
    @Override
    public void deleteDish(String ids) {
        String[] list = ids.split(",");
        for (String id : list) {
            removeById(Long.parseLong(id));
            dishFlavorService.remove(new QueryWrapper<DishFlavor>().eq("dish_id", id));
        }
    }
}
